package com.great.meal.search.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.support.SessionStatus;

import com.great.meal.search.dao.HotelDao;
import com.great.meal.search.domain.Dish;
import com.great.meal.search.domain.Hotel;
import com.great.meal.search.validator.HotelValidator;


/**
 * @author bili
 *
 */
@Controller
@RequestMapping("/hotel.do")
public class HotelController {

	protected final Log logger = LogFactory.getLog(getClass());
	@Autowired
	private  HotelDao hotelDao;
	
	
	
	/**
	 *   判断饭店是否已存在
	 * @param hotelName
	 * @param response
	 * @return
	 */
	@RequestMapping(params = "method=isExist")
	public String isExist(@RequestParam("hotelAccount") String hotelAccount,HttpServletResponse response){
		
		PrintWriter out;
		try {
			out = response.getWriter();
			out.print(false);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;		
	}
	
	/**
	 * 用户登录
	 * @param hotel
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(params = "method=loginhotel")
	public String logonHotel(Hotel hotel,ModelMap model,HttpServletRequest request,HttpServletResponse response){
		
		
		model.addAttribute("hotel",hotel);
		return "hotelInformmation";
	}
	
	/**
	 * 注册饭店
	 * @param hotel
	 * @param result
	 * @param status
	 * @return
	 */
	@RequestMapping(params = "method=addHotel")
	public String addHotel(Hotel hotel, BindingResult result, SessionStatus status){
		
		new HotelValidator().validate(hotel, result);
		if(result.hasErrors()){
			return "registerHotel";
		} else{
			this.hotelDao.saveOrUpdate(hotel);
			status.setComplete();
			return "index";
		}
	}
	
	/**
	 * 修改饭店信息
	 * @param hotelId
	 * @param hotel
	 * @return
	 */
	@RequestMapping(params = "method=editHotel")
	public String edithotel(@RequestParam("hotelId") String hotelId,Hotel hotel){
		
		
		
		return null;
		
	}
	
	/**
	 * 删除饭店
	 * @return
	 */
	@RequestMapping(params = "method=deleteHotel")
	public String edithotel(){
		
		
		
		return null;
		
	}
	
	/**
	 * 饭店增加菜单项
	 * @param hotel
	 * @param dish
	 * @return
	 */
	@RequestMapping(params = "method=hotelAddDish")
	public String hotelAddDish(Hotel hotel, Dish dish){
		Set<Dish> dishs =  hotel.getDishes();
		dishs.add(dish);
		hotel.setDishes(dishs);
		
		this.hotelDao.saveOrUpdate(hotel);
		return null;
		
		
	}
	
	@RequestMapping(params = "method=hotelDeleteDish")
	public String hotelDeleteDish(Hotel hotel,Dish dish){
		Set<Dish> dishs =  hotel.getDishes();
		if(dishs.contains(dish)){
			
		}
		
		hotel.setDishes(dishs);
		
		return null;
	}
	
}
